package com.zheng.provider.server;


import com.zheng.provider.pojo.ZhZookeeper;
import com.zheng.provider.service.UserServiceImpl;
import com.zheng.zk.Curator;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(value = 1)
public class ServerBootStrap implements ApplicationRunner {
    @Autowired
    ZhZookeeper zhZookeeper;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        UserServiceImpl.startServer(zhZookeeper.getHost(),zhZookeeper.getPort());
        System.out.println("启动Netty");
        System.out.println("启动端口"+zhZookeeper.getPort());
        registerNode(Curator.getCuratorFramework());
    }

    public  void registerNode(CuratorFramework curator) throws Exception {
         //注册临时节点到zk
         //此处0代表连接时间
          String data="host:"+zhZookeeper.getHost()+":port:"+zhZookeeper.getPort()+":0";
          String path="/zk-zheng-"+zhZookeeper.getName();
          curator.create().withMode(CreateMode.EPHEMERAL).forPath(path,data.getBytes());


    }
}
